package com.ogms.modelservice.customer;

import com.ogms.modelservice.*;

import java.util.ArrayList;
import java.util.List;

public class RunModelService {

    private  String ip;

    private  String port;

    public RunModelService(String ip,String port) {
        this.ip = ip;
        this.port = port;
        this.server1 =ServerFactory.createServer(ip, Integer.parseInt(port) );
    }
    private  Server server1;
//    private  Server server1= ServerFactory.createServer("172.21.212.119", 8060);

    public Data uploadData(String inputFileName){

        ServiceAccess pServiceAccess = server1.getServiceAccess();
        Data pData = pServiceAccess.uploadDataByFile(inputFileName, "Input.inp");
        return pData;
    }

    public String getRecordId(Data pData,String serviceId){

        ServiceAccess pServiceAccess = server1.getServiceAccess();
        DataConfiguration pDataconfig = pServiceAccess.createDataConfig();
        pDataconfig.insertData("9949226b-958f-453f-8bc4-a8a03246d412", "inpData", pData.getID(), false);
        ModelService pMservice = pServiceAccess.getModelServiceByOID(serviceId);
        String recordid = pMservice.invoke(pDataconfig);
        return recordid;
    }

    public  List<Data> runModel(String recordid){
        Server server = ServerFactory.createServer(ip, Integer.parseInt(port));
        ServiceAccess pServiceAccess = server.getServiceAccess();
        ModelServiceRecord pRecord = pServiceAccess.getModelServiceRecordByID(recordid);
        int msrstatus = pRecord.getStatus().getCode();
        while (msrstatus==0) {
            msrstatus = pRecord.getStatus().getCode();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            pRecord.refresh();
            System.out.println("正在运行");
        }

        System.out.println("Swmm model has been finished!");
        List<Data> outputDataList=new ArrayList<>();

        for (int t = 0; t < pRecord.getOutputData().getCount(); t++) {
            DataConfigItem item = pRecord.getOutputData().getItem().get(t);
            String dataid = item.data;
            Data outputdata = pServiceAccess.getDataServiceByID(dataid);
            outputDataList.add(outputdata);
        }
        return  outputDataList;
    }




/*    public List<Data> runModel(String inputData)
    {

        Server server = ServerFactory.createServer("172.21.212.119", 8060);
		if(server.connect() == 1) {
            ServiceAccess pServiceAccess = server.getServiceAccess();
            if (pData != null) {
                System.out.println(pData.getID() + " - " + pData.getSize() + " - " + pData.getGenerationDateTime() + " - " + pData.getTag());
                DataConfiguration pDataconfig = pServiceAccess.createDataConfig();
                pDataconfig.insertData("9949226b-958f-453f-8bc4-a8a03246d412", "inpData", pData.getID(), false);
                ModelService pMservice = pServiceAccess.getModelServiceByOID("5c093ee664e07206e0e720f9");
                String recordid = pMservice.invoke(pDataconfig);


                System.out.println(recordid);
                ModelServiceRecord pRecord = pServiceAccess.getModelServiceRecordByID(recordid);
                System.out.println(pRecord);


                //System.out.println("fuck no bug!!!");
                int msrstatus = pRecord.getStatus().getCode();
                System.out.println("model status is " + msrstatus);
                // TODO 为什么在运行过程中线程被动打断
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                while (msrstatus == 0) {
//                    try {
//                        Thread.sleep(2000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    int a = pRecord.refresh();
                    //打印log信息
                    List<RunningLog> list_logs = pRecord.getLogs();
                    for (int j = 0; j < list_logs.size(); j++) {
                        RunningLog log = list_logs.get(j);
                        System.out.println(log.getType() + " - " + log.getState() + " - " + log.getEvent() + " - " + log.getMessage());
                    }
                    msrstatus = pRecord.getStatus().getCode();
                    System.out.println("Model Run Status is " + msrstatus);

                }
                System.out.println("Swmm model has been finished!");
                List<Data> outputDataList=new ArrayList<>();
                //发送流到前台
                for (int t = 0; t < pRecord.getOutputData().getCount(); t++) {
                    DataConfigItem item = pRecord.getOutputData().getItem().get(t);
                    String dataid = item.data;
                    Data outputdata = pServiceAccess.getDataServiceByID(dataid);
                    outputDataList.add(outputdata);

                    //直接保存到本地
                    String datavalue = outputdata.getValue();
                    //根据value值获取文件后缀
                    String ext = datavalue.substring(datavalue.lastIndexOf(".") + 1);
                    System.out.println(ext);

                    String downLoadFileFullPath="E:\\NativeTest\\TestData\\TouchAir\\" + item.event + "." + ext;

                    try {
                        outputdata.saveAs(downLoadFileFullPath);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                return outputDataList;
            }
        }
    return null;
    }*/

    public List<String> getOutputUrls(List<Data> outputDataList)
    {
        if (outputDataList!=null)
        {
            List<String> urlList=new ArrayList<>();
            for (Data data:outputDataList) {
                urlList.add(data.getUrl());
            }
            return urlList;
        }
        return null;
    }
}
